package com.tengju.bff.interfaces.manage.crm.staff.sentiment.facade;

import com.beust.jcommander.internal.Lists;
import com.beust.jcommander.internal.Maps;
import com.fasterxml.jackson.core.type.TypeReference;
import com.tengju.bff.interfaces.manage.crm.staff.sentiment.form.StaffSentimentCountListQuery;
import com.tengju.bff.interfaces.manage.crm.staff.sentiment.form.StaffSentimentCountPieQuery;
import com.tengju.bff.interfaces.manage.crm.staff.sentiment.form.StaffSentimentRecordListQuery;
import com.tengju.bff.interfaces.manage.crm.user.vo.NicknameVO;
import com.tengju.bff.interfaces.shared.*;
import com.tengju.bff.interfaces.shared.render.ChartVO;
import com.tengju.bff.interfaces.shared.render.PieVO;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.data.domain.model.analysis.Meta;
import com.tengju.data.domain.model.analysis.MetaHolder;
import com.tengju.data.domain.model.user.StarLevelEnum;
import com.tengju.data.domain.shared.DateUtil;
import com.tengju.data.domain.shared.JsonUtil;
import com.tengju.user.application.user.UserDocumentService;
import com.tengju.user.application.user.dto.UserSearchForm;
import com.tengju.user.domain.model.department.DepartmentTree;
import com.tengju.user.domain.model.doc.UserDocument;
import com.tengju.user.domain.model.sentiment.Sentiment;
import com.tengju.user.domain.model.sentiment.SentimentId;
import com.tengju.user.domain.model.sentiment.SentimentRepository;
import com.tengju.user.domain.model.staff.StaffSentimentCount;
import com.tengju.user.domain.model.staff.StaffSentimentRecord;
import com.tengju.user.domain.model.user.SsoUserId;
import com.tengju.user.domain.model.user.StaffInfo;
import com.tengju.user.domain.model.user.StaffSimpleInfo;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.service.DepartmentExternalService;
import com.tengju.user.domain.shared.FormComponent;
import com.tengju.user.domain.shared.FormComponentType;
import com.tengju.user.domain.shared.HtmlConvertUtil;
import com.tengju.user.domain.shared.Row;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: 李大仙
 * @Date: 2021/4/21 2:54 下午
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class StaffSentimentFacade {

    private final DepartmentExternalService departmentExternalService;
    private final SentimentRepository sentimentRepository;
    private final UserDocumentService userDocumentService;
    private final MetaHolder metaHolder;

    @Value("${user.sentiment.details.form}")
    String sentimentDetailsFormModelString;


    public List<KVVO<String, Long>> getServiceProviderInfoByKeywords(String serviceProviderIdAndNicknameAndMobile) {
        List<UserIdCode> genSecIdCodes = ServletContextUtil.getServletContext().getGenSecIdCodes();
        if (CollectionUtils.isEmpty(genSecIdCodes)) {
            return null;
        }
        UserSearchForm form = new UserSearchForm();
        form.setServiceProviderIdAndNicknameAndMobile(serviceProviderIdAndNicknameAndMobile);
        Page<UserDocument> search = userDocumentService.search(form);
        if (search == null) {
            return Lists.newArrayList();
        }
        return search.stream().filter(v -> v.getStarLevel() > 0).map(v -> {
            String nickName = new NicknameVO(v.getNickName(), v.getMobile()).toString();
            return KVVO.of(nickName + " " + StarLevelEnum.parseByStarLevel(v.getStarLevel()).getDesc() + " " + "(" + v.getIdCode() + ") ", v.getIdCode());
        }).collect(Collectors.toList());

    }


    public ChartVO getSentimentRecordChartMonth(String month) {
        ChartVO chart = new ChartVO("月客情记录趋势");
        // 获取departmentIds
        List<DepartmentTree> departmentTree = departmentExternalService.getDepartmentTree();
        List<Long> departmentIdList = departmentTree.stream().map(v -> v.getValue().getValue()).collect(Collectors.toList());
        List<StaffSimpleInfo> staffList = departmentExternalService.getStaffSimpleInfosByDepartmentIds(departmentIdList);
        List<SsoUserId> ssoUserIds = staffList.stream().map(StaffSimpleInfo::getSsoUserId).collect(Collectors.toList());
        LocalDateTime startMonthMin = DateUtil.getLocalDateOfDayMin(DateUtil.formatToLocalDate(month, DateUtil.DATE_MONTH_FORMAT));
        LocalDateTime endMonthMax = DateUtil.getLocalDateOfDayMax(DateUtil.formatToLocalDate(month, DateUtil.DATE_MONTH_FORMAT).plusMonths(1L).plusDays(-1L));
        List<LocalDate> localDateBetween = DateUtil.getDayIntBetween(startMonthMin, endMonthMax);
        Collection<Meta> metas = metaHolder.listMeta("month_sentiment_record_chart").values();
        List<Row> rows = sentimentRepository.monthSentimentRecordChartAnalysis(ssoUserIds, localDateBetween, startMonthMin, endMonthMax);
        List<String> dayList = rows.stream()
                .map(v -> DateUtil.parseStringToMMDD(String.valueOf(v.getValue("day")))).collect(Collectors.toList());
        List<Object> sentimentCount = rows.stream().map(v -> v.getValue("sentimentCount")).collect(Collectors.toList());
        chart.setXAxisdata(dayList);
        chart.buildSeries(sentimentCount, metas);
        return chart;
    }

    public PieVO getSentimentCountPie(StaffSentimentCountPieQuery query) {
        PieVO pie = new PieVO("销售人员分布");
        List<SsoUserId> ssoUserIds;
        List<StaffSimpleInfo> staffList;
        // 获取departmentIds
        List<DepartmentTree> departmentTree = departmentExternalService.getDepartmentTree();
        List<Long> departmentIdList = departmentTree.stream().map(v -> v.getValue().getValue()).collect(Collectors.toList());
        staffList = departmentExternalService.getStaffSimpleInfosByDepartmentIds(departmentIdList);
        ssoUserIds = staffList.stream().map(StaffSimpleInfo::getSsoUserId).collect(Collectors.toList());
        LocalDateTime startDayMin = DateUtil.getLocalDateOfDayMin(DateUtil.formatToLocalDate(query.getStartTime(), DateUtil.DATE_DAY_FORMAT));
        LocalDateTime nextDayMin = DateUtil.getLocalDateOfDayMin(DateUtil.formatToLocalDate(query.getStartTime(), DateUtil.DATE_DAY_FORMAT).plusDays(1L));
        List<LocalDate> localDateBetween = DateUtil.getDayIntBetween(startDayMin, startDayMin);
        List<StaffSentimentCount> staffSentimentCountList = sentimentRepository.getStaffSentimentCount(localDateBetween, ssoUserIds, startDayMin, nextDayMin, false, 0, 200);
        List<StaffSentimentCount> customerLessCollect = staffSentimentCountList.stream().filter(v -> v.getCustomerCount() > 0 && v.getCustomerCount() <= 5).collect(Collectors.toList());
        List<StaffSentimentCount> customerMoreCollect = staffSentimentCountList.stream().filter(v -> v.getCustomerCount() > 5).collect(Collectors.toList());

        int customerLessCount = customerLessCollect.size();
        int customerMoreCount = customerMoreCollect.size();
        int staffTotal = ssoUserIds.size();
        int sentimentRecordZeroOfCustomerCount = staffTotal - customerLessCount - customerMoreCount;

        pie.add("无记录数", sentimentRecordZeroOfCustomerCount)
                .add("有记录数(客户少于等于5人)", customerLessCount)
                .add("有记录数(客户超过5人)", customerMoreCount);
        return pie;
    }

    public FormVO getSentimentRecordDefaultForm() {
        FormVO formVO = new FormVO();
        List<FormComponent> formComponentList = JsonUtil.toList(sentimentDetailsFormModelString, new TypeReference<>() {
        });
        formVO.setFormComponentVOList(formComponentList);
        return formVO;
    }

    public FormVO getDefaultUpdateForm(Long sentimentId) {
        FormVO formVO = new FormVO();
        List<FormComponent> formDefaultComponentVOList = JsonUtil.toList(sentimentDetailsFormModelString, new TypeReference<>() {
        });
        if (CollectionUtils.isEmpty(formDefaultComponentVOList)) {
            throw new InterfaceException(InterfaceCode.SYS_ERROR);
        }
        Map<String, FormComponent> codeFormComponentMap = formDefaultComponentVOList.stream()
                .collect(Collectors.toMap(FormComponent::getCode, v1 -> v1, (v2, v3) -> v2));
        List<FormComponent> formComponentList = getSentimentRecordDetail(sentimentId).getFormComponentVOList();

        for (FormComponent form : formComponentList) {
            if (codeFormComponentMap.containsKey(form.getCode())) {
                form.setType(codeFormComponentMap.get(form.getCode()).getType());
            }
        }

        formVO.setFormComponentVOList(formComponentList);
        return formVO;
    }


    public FormVO getSentimentRecordDetail(Long userSentimentId) {
        FormVO formVO = new FormVO();
        Sentiment sentiment;
        SentimentId sentimentId;
        UserDocument serviceProviderInfo = new UserDocument();
        List<FormComponent> sentimentContextForm;
        List<FormComponent> sentimentResultDetailsForm = new ArrayList<>();

        sentimentId = Optional.ofNullable(userSentimentId).map(SentimentId::new).orElse(null);
        sentiment = sentimentRepository.getSentimentRecordById(sentimentId);
        sentimentContextForm = sentiment.getFormComponentList();

        if (CollectionUtils.isEmpty(sentimentContextForm)) {
            List<FormComponent> sentimentDefaultDetailsForm = JsonUtil.toList(sentimentDetailsFormModelString, new TypeReference<>() {
            });
            FormComponent.batchConvertComponentTypeToText(sentimentDefaultDetailsForm);
            formVO.setFormComponentVOList(sentimentDefaultDetailsForm);
            return formVO;
        }

        List<UserDocument> userDocumentList = userDocumentService.findByIds(List.of(sentiment.getIdCode()));
        if (!CollectionUtils.isEmpty(userDocumentList)) {
            serviceProviderInfo = userDocumentList.get(0);
        }
        String nickName = new NicknameVO(serviceProviderInfo.getNickName(), serviceProviderInfo.getMobile()).toString();
        String serviceProviderResult = nickName + " "
                + StarLevelEnum.parseByStarLevel(serviceProviderInfo.getStarLevel()).getDesc() + " (" + sentiment.getIdCode() + ") ";

        sentimentResultDetailsForm.add(FormComponent.of("serviceProvider", "服务商", serviceProviderResult));
        sentimentResultDetailsForm.add(FormComponent.of("createTime", "创建时间", DateUtil.formatLocalDateTime(DateUtil.DATE_TIME_FORMAT, sentiment.getCreateTime())));
        sentimentResultDetailsForm.addAll(sentimentContextForm);

        FormComponent.batchConvertComponentTypeToText(sentimentResultDetailsForm);
        formVO.setFormComponentVOList(sentimentResultDetailsForm);
        return formVO;
    }

    public TableVO getSentimentRecordPageList(StaffSentimentRecordListQuery query) {
        TableVO tableVO = new TableVO();
        List<ColumnVO> header = new ArrayList<>();
        header.add(ColumnVO.of("createTime", "添加时间", false));
        header.add(ColumnVO.of("departmentName", "部门", false));
        header.add(ColumnVO.of("staffName", "销售负责人", false));
        header.add(ColumnVO.of("serviceProviderInfo", "服务商", false));
        header.add(ColumnVO.of("talkContent", "沟通内容", false));
        tableVO.setHeader(header);
        LocalDateTime startDayMin = DateUtil.getLocalDateOfDayMin(DateUtil.formatToLocalDate(query.getStartTime(), DateUtil.DATE_DAY_FORMAT));
        LocalDateTime nextDayMin = DateUtil.getLocalDateOfDayMin(DateUtil.formatToLocalDate(query.getEndTime(), DateUtil.DATE_DAY_FORMAT).plusDays(1));
        UserIdCode serviceProviderId = Optional.ofNullable(query.getServiceProviderId()).map(UserIdCode::new).orElse(null);
        List<StaffSimpleInfo> staffList;
        List<SsoUserId> ssoUserIdList;
        if (CollectionUtils.isEmpty(query.getSsoUserIds())){
            if (query.getDepartmentId() != null ) {
                staffList = departmentExternalService.getStaffSimpleInfosByDepartmentId(query.getDepartmentId());
            } else {
                // 获取departmentIdList
                List<DepartmentTree> departmentTree = departmentExternalService.getDepartmentTree();
                List<Long> departmentIdList = departmentTree.stream().map(v -> v.getValue().getValue()).collect(Collectors.toList());
                staffList = departmentExternalService.getStaffSimpleInfosByDepartmentIds(departmentIdList);
            }
            ssoUserIdList = staffList.stream().map(StaffSimpleInfo::getSsoUserId).collect(Collectors.toList());
        }else {
            ssoUserIdList = Optional.ofNullable(query.getSsoUserIds())
                    .map(v -> v.stream().filter(Objects::nonNull).distinct().map(SsoUserId::new)
                            .collect(Collectors.toList())).orElse(Collections.emptyList());
        }
        int offset = query.getOffset();
        int total = sentimentRepository.getStaffSentimentRecordTotal(ssoUserIdList, startDayMin, nextDayMin, serviceProviderId);
        if (offset > total) {
            return tableVO;
        }
        List<StaffSentimentRecord> staffSentimentRecordList = sentimentRepository.getStaffSentimentRecordPageList(ssoUserIdList, startDayMin, nextDayMin,
                serviceProviderId, query.getOffset(), query.getPageSize());
        List<SsoUserId> collect = staffSentimentRecordList.stream().filter(Objects::nonNull).map(StaffSentimentRecord::getSsoUserId).distinct().collect(Collectors.toList());
        List<StaffInfo> staffInfos = departmentExternalService.getStaffInfos(collect);
        Map<SsoUserId, StaffInfo> ssoUserIdAndStaffInfoMap = staffInfos.stream()
                .collect(Collectors.toMap(StaffInfo::getSsoUserId, s -> s, (s1, s2) -> s1));

        List<UserIdCode> userIdCodes = staffSentimentRecordList.stream()
                .map(StaffSentimentRecord::getServiceProviderId).filter(Objects::nonNull).collect(Collectors.toList());
        List<UserDocument> userDocumentList = userDocumentService.findByIds(userIdCodes);
        Map<UserIdCode, UserDocument> ssoUserIdUserDocumentMap = userDocumentList.stream()
                .collect(Collectors.toMap(v -> new UserIdCode(v.getIdCode()), s -> s, (s1, s2) -> s1));

        tableVO.setRecords(buildSentimentRecordList(staffSentimentRecordList, ssoUserIdAndStaffInfoMap, ssoUserIdUserDocumentMap));
        tableVO.setPage(PageResultVO.of(query.getPageIndex(), query.getPageSize(), total));
        return tableVO;
    }

    public TableVO exportRecordList(StaffSentimentRecordListQuery query) {
        query.setOffset(0);
        query.setPageSize(100000);
        TableVO table = getSentimentRecordPageList(query);
        List<FormComponent> formComponentDefaultVOList = JsonUtil.toList(sentimentDetailsFormModelString, new TypeReference<>() {
        });
        if (CollectionUtils.isEmpty(formComponentDefaultVOList)) {
            throw new InterfaceException(InterfaceCode.SYS_ERROR);
        }
        for (FormComponent form : formComponentDefaultVOList) {
            if (!StringUtils.equals(form.getCode(), "talkContent")) {
                table.addColumn(form.getCode(), ColumnVO.of(form.getCode(), form.getTitle(), false));
            }
        }

        table.addColumn("staffName", ColumnVO.of("serviceProviderId","服务商ID"));
        table.addColumn("staffName", ColumnVO.of("serviceProviderStarLevel","服务商等级"));
        table.addColumn("staffName", ColumnVO.of("serviceProviderNickName","服务商昵称"));
        table.removeColumn("serviceProviderInfo");

        table.resolveRow(record -> {
            String createTime = record.get("createTime").toString();
            record.put("createTime", DateUtil.formatToLocalDate(createTime, DateUtil.DATE_DAY_FORMAT));
        });
        return table;
    }


    public TableVO exportRecordWithStatistics(StaffSentimentCountListQuery query) {
        query.setOffset(0);
        query.setPageSize(100000);
        return getCountList(query);
    }

    public TableVO getCountList(StaffSentimentCountListQuery query) {
        TableVO tableVO = new TableVO();
        tableVO.setHeader(List.of(
                ColumnVO.of("createTime", "日期", false),
                ColumnVO.of("departmentName", "部门", false),
                ColumnVO.of("staffName", "销售负责人", false),
                ColumnVO.of("customerCount", "有记录的客户数", false),
                ColumnVO.of("sentimentCount", "客情记录数", false)
        ));
        tableVO.setRecords(Lists.newArrayList());
        tableVO.setPage(PageResultVO.of(query.getPageIndex(), query.getPageSize(), 0));

        List<SsoUserId> ssoUserIds;
        List<StaffSimpleInfo> staffList;
        if (query.getDepartmentId() != null && query.getDepartmentId() > 0) {
            staffList = departmentExternalService.getStaffSimpleInfosByDepartmentId(query.getDepartmentId());
        } else {
            // 获取departmentIdList
            List<DepartmentTree> departmentTree = departmentExternalService.getDepartmentTree();
            List<Long> departmentIdList = departmentTree.stream().map(v -> v.getValue().getValue()).collect(Collectors.toList());
            staffList = departmentExternalService.getStaffSimpleInfosByDepartmentIds(departmentIdList);
        }
        ssoUserIds = staffList.stream().map(StaffSimpleInfo::getSsoUserId).collect(Collectors.toList());
        Map<SsoUserId, StaffSimpleInfo> ssoUserIdStaffSimpleInfoMap = staffList.stream()
                .collect(Collectors.toMap(StaffSimpleInfo::getSsoUserId, s -> s, (s1, s2) -> s1));

        if (!CollectionUtils.isEmpty(query.getSsoUserIds())) {
            if (!CollectionUtils.isEmpty(ssoUserIds)) {
                List<SsoUserId> finalSsoUserIds = ssoUserIds;
                ssoUserIds = query.getSsoUserIds().stream()
                        .map(SsoUserId::new)
                        .filter(finalSsoUserIds::contains)
                        .collect(Collectors.toList());
            } else {
                ssoUserIds = query.getSsoUserIds().stream()
                        .map(SsoUserId::new)
                        .collect(Collectors.toList());
            }
        }
        if (CollectionUtils.isEmpty(ssoUserIds)) {
            return tableVO;
        }

        boolean noRecord = query.isNoRecord();

        LocalDateTime startDayMin = DateUtil.getLocalDateOfDayMin(DateUtil.formatToLocalDate(query.getStartTime(), DateUtil.DATE_DAY_FORMAT));
        LocalDateTime end = DateUtil.format(query.getEndTime(), DateUtil.DATE_DAY_FORMAT);
        LocalDateTime nextDayMin = DateUtil.getLocalDateOfDayMin(DateUtil.formatToLocalDate(query.getEndTime(), DateUtil.DATE_DAY_FORMAT).plusDays(1));
        if (startDayMin.plusMonths(2L).isBefore(nextDayMin)){
            throw new InterfaceException(InterfaceCode.DATETIME_BETWEEN_ILLEGAL);
        }
        List<LocalDate> localDateBetween = DateUtil.getDayIntBetween(startDayMin, end);
        if (CollectionUtils.isEmpty(localDateBetween)) {
            return tableVO;
        }
        int total = sentimentRepository.getStaffSentimentCountTotal(localDateBetween, ssoUserIds,
                startDayMin, nextDayMin, noRecord);
        int offset = query.getOffset();
        if (offset > total) {
            return tableVO;
        }
        List<StaffSentimentCount> staffSentimentCountPageList = sentimentRepository.getStaffSentimentCountPageList(localDateBetween, ssoUserIds,
                startDayMin, nextDayMin, noRecord, query.getOffset(), query.getPageSize());
        tableVO.setRecords(buildRecords(staffSentimentCountPageList,  ssoUserIdStaffSimpleInfoMap));
        tableVO.setPage(PageResultVO.of(query.getPageIndex(), query.getPageSize(), total));
        return tableVO;
    }

    private List<Map<String, Object>> buildRecords(List<StaffSentimentCount> staffSentimentCountList,
                                                   Map<SsoUserId, StaffSimpleInfo> ssoUserIdStaffSimpleInfoMap) {
        List<Map<String, Object>> records = Lists.newArrayList();
        if (CollectionUtils.isEmpty(staffSentimentCountList)) {
            return records;
        }
        for (StaffSentimentCount staffSentimentCount : staffSentimentCountList) {
            SsoUserId ssoUserId = new SsoUserId(staffSentimentCount.getSsoUserId());
            StaffSimpleInfo staffSimpleInfo = ssoUserIdStaffSimpleInfoMap.get(ssoUserId);
            if (staffSimpleInfo == null) {
                continue;
            }
            Map<String, Object> row = Maps.newHashMap();
            row.put("createTime", staffSentimentCount.getDay().toString());
            row.put("departmentName", staffSimpleInfo.getDepartmentName());
            String staffName = staffSimpleInfo.getRealName();
            if (!StringUtils.isEmpty(staffSimpleInfo.getNickname())) {
                staffName = staffSimpleInfo.getRealName() + "（" + staffSimpleInfo.getNickname() + "）";
            }
            row.put("ssoUserId", staffSimpleInfo.getSsoUserId().getValue());
            row.put("departmentId", staffSimpleInfo.getDepartmentId());
            row.put("staffName", staffName);
            row.put("customerCount", staffSentimentCount.getCustomerCount());
            row.put("sentimentCount", staffSentimentCount.getSentimentCount());
            records.add(row);
        }
        return records;
    }

    private List<Map<String, Object>> buildSentimentRecordList(List<StaffSentimentRecord> staffSentimentRecordList,
                                                               Map<SsoUserId, StaffInfo> ssoUserIdAndStaffInfoMap,
                                                               Map<UserIdCode, UserDocument> ssoUserIdUserDocumentMap) {
        List<Map<String, Object>> records = Lists.newArrayList();
        if (CollectionUtils.isEmpty(staffSentimentRecordList)) {
            return records;
        }
        for (StaffSentimentRecord staffSentimentRecord : staffSentimentRecordList) {
            Map<String, Object> row = Maps.newHashMap();

            StaffInfo staffInfo = ssoUserIdAndStaffInfoMap.get(new SsoUserId(staffSentimentRecord.getSsoUserId().getValue()));
            UserDocument userDocument = null;
            if (staffSentimentRecord.getServiceProviderId() != null) {
                userDocument = ssoUserIdUserDocumentMap.get(new UserIdCode(staffSentimentRecord.getServiceProviderId().getValue()));
            }
            row.put("createTime", DateUtil.formatLocalDateTime(DateUtil.DATE_TIME_FORMAT, staffSentimentRecord.getCreateTime()));
            row.put("sentimentId", staffSentimentRecord.getSentimentId());
            if (userDocument != null) {
                String nickName = new NicknameVO(userDocument.getNickName(), userDocument.getMobile()).toString();
                String serviceProviderInfo = nickName + " " + StarLevelEnum.parseByStarLevel(userDocument.getStarLevel()).getDesc()
                        + " (" + userDocument.getIdCode() + ")";
                row.put("serviceProviderInfo", serviceProviderInfo);
                row.put("serviceProviderStarLevel", StarLevelEnum.parseByStarLevel(userDocument.getStarLevel()).getDesc());
                row.put("serviceProviderId", userDocument.getIdCode());
                row.put("serviceProviderNickName", nickName);
            }
            row.put("ssoUserId", Optional.ofNullable(staffInfo).map(v -> v.getSsoUserId().getValue()).orElse(null));
            row.put("departmentName", Optional.ofNullable(staffInfo).map(StaffInfo::getDepartmentFullName).orElse(""));
            row.put("staffName", Optional.ofNullable(staffInfo).map(StaffInfo::getRealName).orElse("")
                    + "（" + Optional.ofNullable(staffInfo).map(StaffInfo::getNickname).orElse("") + "）");
            List<FormComponent> formComponentList = JsonUtil.toList(staffSentimentRecord.getSentimentContext(), new TypeReference<>() {
            });
            if (CollectionUtils.isEmpty(formComponentList)) {
                throw new InterfaceException(InterfaceCode.SYS_ERROR);
            }
            for (FormComponent form : formComponentList) {
                if (form.getType() == FormComponentType.html) {
                    row.put(form.getCode(), HtmlConvertUtil.delHtmlTagAndDealImgAndVideo(String.valueOf(form.getFilledData().toString()),100));
                    continue;
                }
                row.put(form.getCode(), form.getFilledData());
            }
            records.add(row);
        }
        return records;
    }


}
